#include "Util.h"
#include <iostream>
using namespace std;

namespace KVConfig
{
    bool is_empty_string(const string &str)
    {
        string::const_iterator iter = str.begin();
        for (; iter != str.end(); ++iter) {
            if (*iter != ' ' && *iter != '\t') {
                return false;
            }
        }
        return true;
    }

    string string_strip(const string &str)
    {
        string::size_type start_pos = str.find_first_not_of(' ');
        string::size_type end_pos = str.find_last_not_of(' ');
        if (start_pos == string::npos) {
            return string();
        } else {
            string ret = str.substr(start_pos, end_pos-start_pos+1);
            return ret;
        }
    }

    /**
     * ====== Test ======
     */

    int util_test_is_empty_string()
    {
        int ret = 0;
        cout << "test_is_empty_string:" << endl;
        if (is_empty_string("  \t ")) {
            cout << "Pass 1" << endl;
        } else {
            cout << "Fail 1" << endl;
            ret = -1;
        }

        if (!is_empty_string(" @  ")) {
            cout << "Pass 2" << endl;
        } else {
            cout << "Fail 2" << endl;
            ret = -1;
        }
        return ret;
    }


    int util_test_string_strip()
    {
        cout << "test_string_strip:" << endl;
        int ret = 0;
        if (string_strip("     a  b     ") == "a  b") {
            cout << "Pass 1" << endl;
        } else {
            cout << "Fail 1" << endl;
            ret = -1;
        }

        if (string_strip("              ") == "") {
            cout << "Pass 1" << endl;
        } else {
            cout << "Fail 1" << endl;
            ret = -1;
        }
        return ret;
    }

    int util_test()
    {
        int ret = 0;
        if (util_test_is_empty_string() != 0)
            ret = -1;
        if (util_test_string_strip() != 0)
            ret = -1;
        return ret;
    }
}
